package data_structure.video;

class SingleLinkedListDemo {
    public static void main(String[] args) {
        HeroNode heroNode1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode heroNode2 = new HeroNode(2,"卢俊义","玉麒麟");
        HeroNode heroNode3 = new HeroNode(3,"吴用","智多星");
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.addList(heroNode1);
        singleLinkedList.addList(heroNode2);
        singleLinkedList.addList(heroNode3);
        singleLinkedList.selectList();
        System.out.println("====修改数据====");
        HeroNode heroNode4 = new HeroNode(2,"小马哥","卷王");
        singleLinkedList.update(heroNode4);
        singleLinkedList.selectList();
        System.out.println();
        singleLinkedList.del(1);
        singleLinkedList.del(2);
        singleLinkedList.del(3);
        singleLinkedList.selectList();
    }
}

//定义SingleLinkedList 管理英雄
class SingleLinkedList{
    //先初始化头节点,头节点不给任何数据
    private HeroNode head = new HeroNode(0,"","");

    //删除节点
    //1.head 不能动，所以我们需要一个temp辅助节点找到待删除结点的前一个节点
    //2.比较的时候是temp.next.no 和 被删除的节点 no 进行比较
    public void del(int no){
        HeroNode temp = head;
        boolean flag = false;//表示是否找到待删除节点
        while (true){
            if (temp.next == null){
                break;
            }
            if (temp.next.no == no){
                //找到待删除节点的前一个节点
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag){
            temp.next = temp.next.next;
        }else
            System.out.println("要删除的节点不存在");
    }

    //对节点数据进行修改
    //简单来说就是创建一个新的对象然后进行替换
    public void update(HeroNode heroNode){
        //判断是否为空
        if(head.next == null){
            System.out.println("链表为空...");
            return;
        }
        HeroNode temp = head.next;
        //标注是否找到需要修改的数据
        boolean flag = false;
        while (true){
            if (temp == null){
                break;
            }
            if(temp.getNo() == heroNode.getNo()){
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag){
            //找到对应位置
            temp.setName(heroNode.getName());
            temp.setNickname(heroNode.getNickname());
        }else
            System.out.println("没有找到指定数据...");
    }

    //添加结点到单向链表
    //不考虑编号顺序时
    //1.找到当前链表的最后节点
    //2.找最后这个节点的next 指向新的节点
    public void addList(HeroNode heroNode){
        //因为 head 节点不能动，所以需要一个辅助 遍历 temp
        HeroNode temp = head;
        while (true){
            if (temp.next == null){
                break;
            }
            temp = temp.next;
        }
        temp.next = heroNode;
    }

    //显示链表【遍历】
    public void selectList(){
        //判断链表是否为空
        if (head.next == null){
            System.out.println("链表为空");
            return;
        }
        //由于头节点，不能动，所以需要一个辅助变量遍历
        HeroNode temp = head.next;
        while (true){
            if (temp == null)
                break;
            System.out.println(temp);
            temp = temp.next;
        }
    }
}

//定义实体类,每个HeroNode 对象就是一个节点
class HeroNode{
    public int no;
    public String name;
    public String nickname;
    public HeroNode next;//指向下一个节点

    public HeroNode(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}
